<?php
/**
 *	[TWCMS] (C)2012 TongWang Inc.
 *	$Id: content_show.php 2 2013-04-27 07:14:17Z nanown@qq.com $
 */
class content_show extends admin{
	public $paging;

	function __construct() {
		parent::__construct();
		$this->main();
	}

	function main() {
		if(empty($_GET['do'])) {
			include load_tpl(__CLASS__);
		}elseif(!empty($_POST)) {
			if($_GET['do'] == 'ajax_add') {
				$this->ajax_add();
			}elseif($_GET['do'] == 'ajax_edit') {
				$this->ajax_edit();
			}elseif($_GET['do'] == 'ajax_del') {
				$this->ajax_del();
			}
		}elseif($_GET['do'] == 'add') {
			$this->title = '内容发布';
			$this->place .= ' &#187 '.$this->title;
			$this->form = ADMNAME.'?mod=content&action=show&do=ajax_add';

			include load_tpl(__CLASS__.'_add');
		}elseif($_GET['do'] == 'edit') {
			$id = intval($_GET['id']);
			$this->title = '内容编辑';
			$this->place .= ' &#187 '.$this->title;
			$this->form = ADMNAME.'?mod=content&action=show&do=ajax_edit&id='.$id;

			$data = $this->db->fetch_first('SELECT * FROM `'.TW_DBPRE.'article` A LEFT JOIN `'.TW_DBPRE.'article_data` D ON A.id=D.id WHERE A.id='.$id.' LIMIT 1');

			include load_tpl(__CLASS__.'_edit');
		}
	}

	#内容发布
	function ajax_add() {
		$tips = '';
		$ret = false;
		$art = $data = array();

		$art['cid'] = intval($_POST['cid']);
		$art['title'] = trim($_POST['title']);
		$data['content'] = trim($_POST['content']);

		if(empty($art['cid'])) {
			$tips = '请选择分类';
		}elseif(empty($art['title'])) {
			$tips = '请填写标题';
		}elseif(strlen($art['title']) < 4) {
			$tips = '标题太短,请增加标题字数';
		}elseif(empty($data['content'])) {
			$tips = '请填写内容';
		}elseif($this->is_disabled_category($art['cid'])) {
			$tips = '非法分类ID';
		}else{
			$art['direction'] = trim(dstr(preg_replace('/\s\s+/', ' ', strip_tags($data['content'])), 200, ''));
			$art['pic'] = trim($_POST['pic']);
			$art['uid'] = $this->auser['uid'];
			$art['username'] = $this->auser['user'];
			$art['time'] = time();
			$art['view'] = 0;
			$art['ip'] = ip();
			$art['status'] = 1;

			if($this->db->insert('article',$art)) {
				$data['id'] = $this->db->insert_id();
				$attach = array('cid'=>$art['cid'], 'mid'=>3, 'contentid'=>$data['id']);
				if($this->db->insert('article_data',$data) && $this->set_attachment($attach) && $this->set_category_count($art['cid'], 'article') && $this->set_member_posts()) {
					$ret = true;
					$tips = '发布成功';
				}else{
					$tips = '发布失败';
				}
			}else{
				$tips = '写入数据库失败';
			}
		}
		ret_json($ret, $tips);
	}

	#内容编辑
	function ajax_edit() {
		$tips = '';
		$ret = false;
		$art = $data = array();

		$id = intval($_GET['id']);
		$art['cid'] = intval($_POST['cid']);
		$art['title'] = trim($_POST['title']);
		$data['content'] = trim($_POST['content']);
		$art['pic'] = trim($_POST['pic']);

		if(empty($id)) {
			$tips = 'ID不正确';
		}elseif(empty($art['title'])) {
			$tips = '请填写标题';
		}elseif(strlen($art['title']) < 4) {
			$tips = '标题太短,请增加标题字数';
		}elseif(empty($data['content'])) {
			$tips = '内容不能为空';
		}elseif($this->is_disabled_category($art['cid'])) {
			$tips = '非法分类ID';
		}else{
			$art['direction'] = trim(dstr(preg_replace('/\s\s+/', ' ', strip_tags($data['content'])), 200, ''));

			if($this->db->update('article', $art, 'id='.$id)) {
				$attach = array('cid'=>$art['cid'], 'mid'=>3, 'contentid'=>$id);
				if($this->db->update('article_data', $data, 'id='.$id) && $this->set_attachment($attach)) {
					$ret = true;
					$tips = '修改成功';
				}else{
					$tips = '修改失败';
				}

			}else{
				$tips = '修改数据库失败';
			}
		}
		ret_json($ret, $tips);
	}

	#删除内容
	function ajax_del() {
		$tips = '';
		$err = 1;

		$id = intC($_POST, 'id');
		$cid = intC($_POST, 'cid');

		if(empty($id)) {
			$msg = 'ID不正确';
		}elseif(empty($cid)) {
			$msg = 'CID不正确';
		}else{
			if($this->db->query('DELETE FROM `'.TW_DBPRE.'article` WHERE `id`='.$id) && $this->db->query('DELETE FROM `'.TW_DBPRE.'article_data` WHERE `id`='.$id) && $this->set_category_count($cid, 'article') && $this->set_member_posts()) {
				$err = 0;
				$msg = '删除成功';
			}else{
				$msg = '删除失败';
			}
		}
		exit('{"err":"'.$err.'","msg":"'.$msg.'"}');
	}

	#更新用户发布内容数
	function set_member_posts() {
		return $this->db->query('UPDATE `'.TW_DBPRE.'member` SET `posts`=(SELECT count(*) FROM `'.TW_DBPRE.'article` WHERE `uid`='.$this->auser['uid'].') WHERE `uid`='.$this->auser['uid']);
	}

	#更新分类内容数
	function set_category_count($cid, $table='article') {
		return $this->db->query('UPDATE `'.TW_DBPRE.'category` SET `count`='.$this->get_content_count($cid, $table).' WHERE cid='.$cid);
	}

	#修改附件所属 cid mid contentid
	function set_attachment($arr) {
		return $this->db->update('attachment', $arr, 'contentid=0');
	}

	#获取内容数
	function get_content_count($cid, $table='article') {
		return $this->db->result_first('SELECT count(*) FROM  `'.TW_DBPRE.$table.'` WHERE cid='.$cid);
	}

	#获取内容
	function get_content() {
		#初始参数
		$pagenum = 50;
		$total = intget('total');
		$page = intget('page',1);

		if(empty($total)) {
			$total = $this->db->result_first('SELECT count(id) FROM `'.TW_DBPRE.'article`');
		}

		$this->paging = page($total, $page, $pagenum, '?mod=content&action=show&total='.$total.'&page=%s');

		return $this->db->fetch_all('SELECT * FROM `'.TW_DBPRE.'article` ORDER BY `id` DESC LIMIT '.($page-1)*$pagenum.', '.$pagenum);
	}

	#获取分类SELECT
	function get_category_select($upid=0) {
		echo '<option value="0">请选择</option>';
		foreach($this->get_category() as $lv) {
			$selected = isset($_GET['cid']) && $_GET['cid']==$lv['cid'] ? ' selected="selected"' : '';
			$disabled = $lv['mid']<3 || $lv['isindex'] ? ' disabled="disabled"' : '';
			$strpre = '';
			if($disabled) {
				if($lv['mid'] == 1)  $strpre = ' [链接]';
				elseif($lv['mid'] == 2)  $strpre = ' [单页]';
				else $strpre = ' [频道]';
			}
			echo '<option value="'.$lv['cid'].'"'.$selected.$disabled.'>'.str_repeat("　", $lv['pre']).'|─'.$lv['categoryname'].$strpre.'</option>';
		}
	}

	#获取分类
	function get_category($upid=0, $pre=-1) {
		$sql = 'SELECT * FROM  `'.TW_DBPRE.'category` WHERE upid='.$upid.' ORDER BY `orderby` ASC';
		$query = $this->db->query($sql);

		++$pre;
		$arr = array();
		while($row=$this->db->fetch_array($query)) {
			$row['pre'] = $pre;
			$arr[] = $row;

			$child = $this->get_category($row['cid'], $pre);
			if($child) $arr = array_merge($arr, $child);
		}
		return $arr;
	}

	#判断是否为禁止添加内容的分类
	function is_disabled_category($cid) {
		$row = $this->db->fetch_first('SELECT mid,isindex FROM `'.TW_DBPRE.'category` WHERE cid='.$cid);
		return($row['mid']<3 || $row['isindex']);
	}
}